package com.example.androidexample.animation;

import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;

import com.example.androidexample.R;

public class XMLAnimationBuilder implements AnimationBuilder, View.OnClickListener {

    private final AnimationActivity activity;
    private RotateAnimation rotateAnimation;
    private AlphaAnimation alphaAnimation;
    private TranslateAnimation translateAnimation;
    private ScaleAnimation scaleAnimation;
    private Button btXmlLoadTranslate;
    private Button btXmlLoadAlpha;
    private Button btXmlLoadScale;
    private Button btXmlLoadRotate;
    @Nullable
    private Animation mCurrentAnimation;

    public XMLAnimationBuilder(AnimationActivity activity) {
        this.activity = activity;
        initView();
        initXmlAnimation();
        setListener();
    }

    private void setListener() {
        btXmlLoadTranslate.setOnClickListener(this);
        btXmlLoadAlpha.setOnClickListener(this);
        btXmlLoadScale.setOnClickListener(this);
        btXmlLoadRotate.setOnClickListener(this);
    }

    private void initXmlAnimation() {
        rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(activity, R.anim.animation_rotate);
        alphaAnimation = (AlphaAnimation) AnimationUtils.loadAnimation(activity,R.anim.animation_alpha);
        translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(activity,R.anim.animation_translate);
        scaleAnimation = (ScaleAnimation) AnimationUtils.loadAnimation(activity,R.anim.animation_scale);

    }

    private void initView() {
        btXmlLoadTranslate = activity.findViewById(R.id.bt_xml_load_translate);
        btXmlLoadAlpha = activity.findViewById(R.id.bt_xml_load_alpha);
        btXmlLoadScale = activity.findViewById(R.id.bt_xml_load_scale);
        btXmlLoadRotate = activity.findViewById(R.id.bt_xml_load_rotate);
    }


    @Override
    public Animation buildAnimation() {
        return mCurrentAnimation;
    }

    @Override
    public void onClick(View v) {
        @IdRes
        int vId = v.getId();
        switch (vId) {
            case R.id.bt_xml_load_translate: {
                mCurrentAnimation = translateAnimation;
            } break;
            case R.id.bt_xml_load_alpha: {
                mCurrentAnimation = alphaAnimation;
            } break;
            case R.id.bt_xml_load_scale: {
                mCurrentAnimation = scaleAnimation;
            } break;
            case R.id.bt_xml_load_rotate: {
                mCurrentAnimation = rotateAnimation;
            } break;
        }
        activity.handleAction(AnimationBuilderType.XML,this);
    }
}
